const ci = require('miniprogram-ci');
const { appList } = require('./config');

let newApplist = [];
for (let i = 0; i < appList.length; i++) {
    let item = appList[i];
    let obj = {
        ...item,
        project: new ci.Project({
            appid: item.appid,
            type: 'miniProgram',
            projectPath: `${process.cwd()}/dists/dist_${item.name}/`,
            privateKeyPath: process.cwd() + `/private_key/${item.appid}.key`,
            ignores: ['node_modules/**/*', '$node_modules/**/*'],
        }),
    };
    newApplist.push(obj);
}

async function upload(
    project,
    version = '1.1.1.0113',
    desc = 'test miniprogram-ci upload',
    pagePath = 'pages/index/index'
) {
    return await ci.upload({
        project,
        version,
        desc,
        setting: {
            es6: true,
            es7: true,
            minify: true,
            autoPrefixWXSS: true,
            minifyWXML: true,
            minifyWXSS: true,
            minifyJS: true,
            codeProtect: true,
        },
        qrcodeFormat: 'image',
        qrcodeOutputDest: './qrcode',
        pagePath, // 预览页面
        searchQuery: '', // 预览参数 [注意!]这里的`&`字符在命令行中应写成转义字符`\&`
        scene: 1011,
        onProgressUpdate: console.log,
    });
}

async function init() {
    // 上传
    for (let i = 0; i < newApplist.length; i++) {
        let item = newApplist[i];
        // console.log(item);
        let uploadResult = await upload(item.project, item.version, item.desc, item.path);
        console.log('uploadResult', uploadResult);
    }
}

module.exports = {
    init: init,
};
